package com.fx.server.impl;

import com.fx.common.UIException;
import com.fx.common.api.FxAuthServer;
import com.fx.common.domain.ImageCode;
import com.fx.common.domain.R;
import com.fx.server.FxUiAuthServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
@Slf4j
@Service
public class FxUiAuthServerImpl implements FxUiAuthServer {

    @Resource
    private FxAuthServer fxAuthServer;

    @Override
    public ByteArrayInputStream currentBase64ImageCode() {
        R<ImageCode> code = fxAuthServer.getImageCode();
        if(StringUtils.equals("0000",code.getCode())){
            String bs = code.getData().getImg();
            return new ByteArrayInputStream(Base64.decodeBase64(bs.trim()));
        }else {
            throw new UIException(code.getMessage(),code.getCode());
        }
    }
}
